/*
 * @Description: localStorage存储
 * @Author: xinqiao lxq1990.0716@qq.com
 * @Date: 2023-04-06
 * @LastEditors: xinqiao lxq1990.0716@qq.com
 * @LastEditTime: 2024-11-12
 */

const TOKEN_KEY = 'token'
const PLATFORMCODE_KEY = 'platformCode'
const COMPANY_INFO_KEY = 'companySiteInfo'
const COMPANY_LIST = 'companyList'
const USER_ID = 'userId'
const USER_INFO = 'userInfo'
const LAST_LOGIN_TIME = 'lastLoginTime'

/**
 * 存储token
 * @param token 用户令牌
 */
export const setToken = (token: string) => localStorage.setItem(TOKEN_KEY, token)

/**
 * 获取token
 * @return
 */
export const getToken = () => localStorage.getItem(TOKEN_KEY) as string

/**
 * 移除token
 */
export const removeToken = () => localStorage.removeItem(TOKEN_KEY)

/**
 * 存储userId
 * @param userId 用户Id
 */
export const setUserId = (userId: string) => localStorage.setItem(USER_ID, userId)

/**
 * 获取userId
 * @return
 */
export const getUserId = () => localStorage.getItem(USER_ID) as string

/**
 * 移除userId
 */
export const removeUserId = () => localStorage.removeItem(USER_ID)

/**
 * 存储平台编码
 * @param token 用户令牌
 */
export const setPlatformCode = (platformCode: string) => localStorage.setItem(PLATFORMCODE_KEY, platformCode)

/**
 * 获取平台编码
 * @return
 */
export const getPlatformCode = () => localStorage.getItem(PLATFORMCODE_KEY) as string

/**
 * 移除平台编码
 */
export const removePlatformCode = () => localStorage.removeItem(PLATFORMCODE_KEY)

/**
 * 存储公司信息
 * @param companyInfo 公司信息
 */
export const setCompanyInfo = (companyInfo: ICompanyInfo) => localStorage.setItem(COMPANY_INFO_KEY, JSON.stringify(companyInfo ?? {}))

/**
 * 获取公司信息
 * @return
 */
export const getCompanyInfo = (): ICompanyInfo => {
  const companyInfo = localStorage.getItem(COMPANY_INFO_KEY)
  return JSON.parse(companyInfo ?? '{}')
}
/**
 * 存储机构列表
 */
export const setCompanyList = (companyInfo: ICompanyInfo) => localStorage.setItem(COMPANY_LIST, JSON.stringify(companyInfo ?? []))
/**
 * 查询本地存储的机构列表
 */
export const getCompanyList = () => {
  const companyList = localStorage.getItem(COMPANY_LIST)
  return JSON.parse(companyList ?? '[]')
}

export const removeCompanyInfo = () => localStorage.removeItem(COMPANY_INFO_KEY)

/**
 * 存储用户信息
 */
export const setUserInfo = (userInfo: IUserInfo) => localStorage.setItem(USER_INFO, JSON.stringify(userInfo ?? {}))

/**
 * 获取用户信息
 * @return
 */
export const getUserInfo = () => {
  const userInfo = localStorage.getItem(USER_INFO)
  return JSON.parse(userInfo ?? '{}')
}

/**
 * 移除用户信息
 */
export const removeUserInfo = () => localStorage.removeItem(USER_INFO)

/**
 * 添加最后登录时间
 */
export const setLastLoginTime = (time) => {
  localStorage.setItem(LAST_LOGIN_TIME, time)
}

/**
 * 获取最后登录时间
 */
export const getLastLoginTime = () => {
  return localStorage.getItem(LAST_LOGIN_TIME) as string
}
